import * as path from 'path'

import { DatabaseTypes, DbmOptions, DbmService } from '../src'

const options: DbmOptions = new DbmOptions()
options.database = DatabaseTypes.mariadb

// Generate database modeling markdown files (.db.md) from a database
options.sourceFolder = path.join(__dirname, `../../temp/tables/v1`)
await DbmService.fromDatabase(
	{
		databaseType: options.database,
		host: '127.0.0.1',
		port: 3306,
		user: 'root',
		password: 'password',
		database: 'my-db',
	},
	options.sourceFolder,
	true,
)

// Read table definition markdow files(.db.md), and produce a sql file with create-table-if-not-exists scripts
options.sourceFolder = path.join(__dirname, '../tests/data/mariadb/tables')
options.targetPath = path.join(__dirname, '../temp/createTableIf.sql')
DbmService.toCreateTableIfNotExistsSql(options)

// Read table definition markdow files(.db.md), and produce a sql file with create-table scripts
options.targetPath = path.join(__dirname, '../temp/createTable.sql')
DbmService.toCreateTableSql(options)

// Read table definition markdow files(.db.md), and produce a sql file with drop-table scripts
options.targetPath = path.join(__dirname, '../temp/dropTable.sql')
DbmService.toDropTableSql(options)

// Read table definition markdow files(.db.md), and produce a sql file with drop-then-create-table scripts
options.targetPath = path.join(__dirname, '../temp/dropThenCreateTable.sql')
DbmService.toDropThenCreateTableSql(options)

// Read table definition markdow files(.db.md), and produce a sql file with upgrade scripts
options.previousVersionFolder = path.join(__dirname, `../tests/data/mariadb/tables/v1`)
options.targetPath = path.join(__dirname, '../temp/upgrade.sql')
DbmService.toUpgradeSql(options)

// Read table definition markdow files(.db.md), and produce a json file
options.targetPath = path.join(__dirname, '../temp/model.json')
DbmService.toJson(options)

// Read table definition markdow files(.db.md), and produce a model object
options.targetPath = ''
const model = DbmService.toModel(options)
// eslint-disable-next-line no-console
console.log(JSON.stringify(model, null, 2))

// Generate database modeling markdown files (.db.md) from a model
DbmService.toMarkdown(model, path.join(__dirname, '../temp/tables'), true)

// Generate index.md from a model
DbmService.genIndexFile(model, options.sourceFolder)
